/*
  学习目标：NavBar支持固定在顶部
*/

import Icon from '@/components/Icon';
import { ReactNode } from 'react';
import { useHistory } from 'react-router-dom';
import styles from './index.module.scss';
import classNames from 'classnames';
type NavBarProps = {
  onBack?: () => void;
  children?: ReactNode;
  right?: ReactNode;
  // 1. 扩展propos属性，来表示NavBar固定
  fixed?: boolean;
};

export default function NavBar({ onBack, children, right, fixed }: NavBarProps) {
  // 2. 解构获得fixed
  const history = useHistory();
  const handleBack = () => {
    if (onBack) {
      return onBack();
    }
    history.goBack();
  };
  return (
    <div className={styles.root}>
      {/* 3. 动态加fixed类型 */}
      <div className={classNames('main', { fixed: fixed })}>
        <div className="left">
          <Icon type="iconfanhui" onClick={handleBack} />
        </div>
        <div className="title">{children}</div>
        <div className="right">{right}</div>
      </div>
    </div>
  );
}
